package com.example.jdimust.main.ui.home;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.jdimust.util.ACache;
import com.example.jdimust.R;

public class HomeFragment extends Fragment {

    static String loname;
    private HomeViewModel homeViewModel;


    @SuppressLint("ValidFragment")
    public HomeFragment(String res_in_all) {
        loname = res_in_all;
        System.out.println(loname);
        }
    //特别注意，千万不可忽略的一步，少了这个构造方法会报错
    public HomeFragment() {
        // Required empty public constructor
    }

        public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View root = inflater.inflate(R.layout.fragment_home, container, false);
            ACache acache= ACache.get(getContext());//创建ACache组件
            String username0 = acache.getAsString("acaname");
//            Gson gson6 = new Gson();
//            System.out.println(loname);
//            LoginBean loginbean = gson6.fromJson(loname,LoginBean.class);
            TextView tvloginmme = (TextView) root.findViewById(R.id.nowuser);
//            String nname = loginbean.getName();
            tvloginmme.setText(username0);
            ToggleButton togbtinfo = (ToggleButton) root.findViewById(R.id.toguser);
            ToggleButton togbtjuri = (ToggleButton) root.findViewById(R.id.togjuri);
            final LinearLayout layoutuser=(LinearLayout) root.findViewById(R.id.userinfo);
            final LinearLayout layoutkuri=(LinearLayout) root.findViewById(R.id.jurisdiction);
            TextView usna = (TextView) root.findViewById(R.id.username);
            TextView user = (TextView) root.findViewById(R.id.user);
            TextView userid = (TextView) root.findViewById(R.id.userid);
            TextView user00 = (TextView) root.findViewById(R.id.user0);
            String userzh = acache.getAsString("acauser111");
            String useridtext = acache.getAsString("acauserid");
            usna.setText(username0);
            user.setText(userzh);
//            String nowid = String.valueOf(loginbean.getId());
            user00.setText(userzh);
            userid.setText(useridtext);

            layoutuser.setVisibility(View.GONE);
            layoutkuri.setVisibility(View.GONE);


            CheckBox checkBox1 = (CheckBox) root.findViewById(R.id.juri_directOperation);
            CheckBox checkBox2 = (CheckBox) root.findViewById(R.id.juri_contractOperation);
            CheckBox checkBox3 = (CheckBox) root.findViewById(R.id.juri_configureRoles);
            CheckBox checkBox4 = (CheckBox) root.findViewById(R.id.juri_authorizeUserRoles);
            CheckBox checkBox5 = (CheckBox) root.findViewById(R.id.juri_setConsensus);
            CheckBox checkBox6 = (CheckBox) root.findViewById(R.id.juri_setCrypto);
            CheckBox checkBox7 = (CheckBox) root.findViewById(R.id.juri_approveTx);
            CheckBox checkBox8 = (CheckBox) root.findViewById(R.id.juri_consensusTx);
            CheckBox checkBox9 = (CheckBox) root.findViewById(R.id.juri_registerParticipant);
            CheckBox checkBox10 = (CheckBox) root.findViewById(R.id.juri_setUserAttributes);
            CheckBox checkBox11 = (CheckBox) root.findViewById(R.id.juri_registerUser);
            CheckBox checkBox12 = (CheckBox) root.findViewById(R.id.juri_registerEventAccount);
            CheckBox checkBox13 = (CheckBox) root.findViewById(R.id.juri_writeEventAccount);
            CheckBox checkBox14 = (CheckBox) root.findViewById(R.id.juri_registerDataAccount);
            CheckBox checkBox15 = (CheckBox) root.findViewById(R.id.juri_writeDataAccount);
            CheckBox checkBox16 = (CheckBox) root.findViewById(R.id.juri_registerContract);
            CheckBox checkBox17 = (CheckBox) root.findViewById(R.id.juri_upgradeContract);

//            int ces1 = Integer.valueOf(acache.getAsString("acadirectOperation")).intValue();


            if (Integer.valueOf(acache.getAsString("acadirectOperation")).intValue() == 1){
                checkBox1.setChecked(true);
            }
//            if (acache.getAsString("acadirectOperation") != "1"){
//                checkBox1.setChecked(true);
//            }
            if (Integer.valueOf(acache.getAsString("acacontractOperation")).intValue() == 1){
                checkBox2.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acaconfigureRoles")).intValue() == 1){
                checkBox3.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acaauthorizeUserRoles")).intValue() == 1){
                checkBox4.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acaetConsensus")).intValue() == 1){
                checkBox5.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acasetCrypto")).intValue() == 1){
                checkBox6.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acaapproveTx")).intValue() == 1){
                checkBox7.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acaconsensusTx")).intValue() == 1){
                checkBox8.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acaregisterParticipant")).intValue() == 1){
                checkBox9.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acasetUserAttributes")).intValue() == 1){
                checkBox10.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acaregisterUser")).intValue() == 1){
                checkBox11.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acaregisterEventAccount")).intValue() == 1){
                checkBox12.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acawriteEventAccount")).intValue() == 1){
                checkBox13.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acaregisterDataAccount")).intValue() == 1){
                checkBox14.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acawriteDataAccount")).intValue() == 1){
                checkBox15.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acaregisterContract")).intValue() == 1){
                checkBox16.setChecked(true);
            }
            if (Integer.valueOf(acache.getAsString("acaupgradeContract")).intValue() == 1){
                checkBox17.setChecked(true);
            }

            System.out.println(acache.getAsString("acadirectOperation")+
                            acache.getAsString("acacontractOperation")+
                            acache.getAsString("acaconfigureRoles")+
                            acache.getAsString("acaauthorizeUserRoles")+
                            acache.getAsString("acaetConsensus")+
                            acache.getAsString("acasetCrypto")+
                            acache.getAsString("acaapproveTx")+
                            acache.getAsString("acaconsensusTx")+
                            acache.getAsString("acaregisterParticipant")+
                            acache.getAsString("acasetUserAttributes")+
                            acache.getAsString("acaregisterUser")+
                            acache.getAsString("acaregisterEventAccount")+
                            acache.getAsString("acawriteEventAccount")+
                            acache.getAsString("acaregisterDataAccount")+
                            acache.getAsString("acawriteDataAccount")+
                            acache.getAsString("acaregisterContract")+
                            acache.getAsString("acaupgradeContract"));







            togbtinfo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    if(isChecked){//开
                        layoutuser.setVisibility(View.GONE);
                    }
                    else{//关
                        layoutuser.setVisibility(View.VISIBLE);
                    }
                }
            });

            togbtjuri.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    if(isChecked){//开
                        layoutkuri.setVisibility(View.GONE);
                    }
                    else{//关
                        layoutkuri.setVisibility(View.VISIBLE);
                    }
                }
            });
            return root;
        }

}